//
// Created by 俊峰 on 2017/5/15.
//

#include "com_encryptHelper.h"
#include <stdio.h>

JNIEXPORT jbyteArray JNICALL
Java_com_zhong_thesimpleconnection_util_NativeHelper_getEncode(JNIEnv *env, jclass type,
                                                               jbyteArray sourceData_) {
    jbyte *data = (*env)->GetByteArrayElements(env, sourceData_, NULL);
    jsize length = (*env)->GetArrayLength(env, sourceData_);
    jsize myMemory = length;
    int set = 0;
    if (length % 6 != 0)
    {
        set = 1;
    }
    length = length / 6 * 8 + set * 8;
    jbyteArray result = (*env)->NewByteArray(env, length);
    jbyte ret[length];
    int time = length / 8;
    int index;
    int i;
    for(index = 0; index < time; index++)
    {
        for (i = 0; i < 8; i++) {
            if (myMemory > index * 8 + i - (index + 1) * 2)
            {
                if (i == 0 || i == 1)
                {
                    ret[index * 8 + i] = 0;
                }
                else
                {
                    ret[index * 8 + i] = data[index * 6 + i - 2];
                }
            }
            else
            {
                ret[index * 8 + i] = 0;
            }
        }
    }
    (*env)->SetByteArrayRegion(env, result, 0, length, ret);
    return result;
}